package mbg;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author luyanpu
 * @Date 2023/6/25
 */
public class MyBatisGen {
    public static void main(String[] args) throws Exception {
//        generator();
        generatorCode();
    }

    /**
     * 使用配置文件xml的方式生成
     * 1.此种生成方式可以通过idea maven窗口中的mybatis-generator 直接生成
     */
    private static void generator() throws Exception {
        File configFile = new ClassPathResource("generatorConfig.xml").getFile();
        List<String> warnings = new ArrayList<String>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        System.out.println("代码生成完毕>>>>>>>>>>>>");

        Configuration configuration = new Configuration();

    }

    /**
     * 使用纯代码的方式进行生成
     */
    private static void generatorCode() throws Exception {
        List<String> warnings = new ArrayList<String>();
        Configuration config = GeneratorConfig.getGeneratorConfig();
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        System.out.println("代码生成完毕>>>>>>>>>>>>");


    }
}
